All files / src/app/api/dev/tickets/[id]/move route.ts

0% Statements 0/59
100% Branches 0/0
0% Functions 0/1
0% Lines 0/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                                                                                                                       
/**
 * Dev Ticket Move API
 * POST /api/dev/tickets/[id]/move - Move ticket to a new parent
 */

import { NextRequest, NextResponse } from 'next/server';
import { Session } from 'next-auth';
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  ApiError,
  ApiSuccessResponse,
  ApiErrorResponse } from '@/lib/api';
import { RouteContext } from '@/lib/api/middleware';
import type { AuthenticatedUser } from '@/lib/api/middleware/types';
import { moveTicketToParent } from '@/lib/dev-ticket';
import { MoveDevTicketSchema } from '@/lib/validation/dev-ticket-schemas';
import { logger } from '@/lib/logging';

interface RouteParams {
  params: Promise<{ id: string }>;
}

async function handlePost(
  request: NextRequest,
  context: RouteContext | undefined,
  session: Session,
  user: AuthenticatedUser
): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> {
  const { id } = await (context as RouteParams).params;
  const body = await request.json();

  const validationResult = MoveDevTicketSchema.safeParse(body);
  if (!validationResult.success) {
    throw ApiError.validation(
      'Validation failed',
      validationResult.error.flatten().fieldErrors
    );
  }

  const { newParentId } = validationResult.data;

  const result = await moveTicketToParent(id, newParentId, user.id);

  if (!result.success) {
    throw ApiError.badRequest(result.error || 'Failed to move ticket');
  }

  logger.info(`Moved ticket ${id} to parent ${newParentId || 'root'}`, {
    category: 'DEV_TICKET_MOVE',
    ticketId: id,
    newParentId,
    userId: user.id});

  return successResponse({ message: 'Ticket moved successfully' });
}

export const POST = withErrorHandling(withAdmin(handlePost));